package com.example.chatdbservice.rabbitmq.consumer;

import com.example.chatdbservice.mapper.ChatSessionMapper;
import com.example.chatdbservice.mapper.ChatSessionUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.constants.Constants;
import org.example.entity.po.ChatSession;
import org.example.entity.po.ChatSessionUser;
import org.example.entity.query.ChatSessionQuery;
import org.example.entity.query.ChatSessionUserQuery;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description
 * @Author susu
 * @Date 2024/10/7
 */
@Component
@Slf4j
public class ChatDBConsumer {

    @Resource
    private ChatSessionMapper<ChatSession, ChatSessionQuery> chatSessionMapper;
    @Resource
    private ChatSessionUserMapper<ChatSessionUser, ChatSessionUserQuery> chatSessionUserMapper;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = Constants.CHATDB_QUEUE),
            exchange = @Exchange(name = Constants.EXCHANGE_CHAT_TOPIC_NAME,type = ExchangeTypes.TOPIC),
            key = {Constants.CHATDB_QUEUE_ROUTING_KEY}
    ))
    public void listenUserQueue(Object msg){
//        ServiceMsgSendEachDto dto = (ServiceMsgSendEachDto) msg;
        if (msg instanceof ChatSession){
            ChatSession chatSession = (ChatSession)msg;
            chatSessionMapper.insertOrUpdate(chatSession);
        }
        if (msg instanceof ChatSessionUser){
            ChatSessionUser chatSessionUser = (ChatSessionUser) msg;
            chatSessionUserMapper.insertOrUpdate(chatSessionUser);
        }
    }
}
